package com.heima.kafka.listener;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.User;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class HelloListener {
// 监听消息
//@KafkaListener(topics = {"user-topic", "order-topic", "payment-topic"})
//@KafkaListener(topics = "user-topic,order-topic,payment-topic")
//可以写多个监听
@KafkaListener(topics = "user-topic")//,itcast-topic 监听(订阅)主题，默认情况下，监听的是所有分区
    public void onMessage(String message){
        if(!StringUtils.isEmpty(message)){
            //HelloController把对象转为json字符串，HelloListener接收的时候再转回对象
            User user = JSON.parseObject(message, User.class);
//            System.out.println(message);
            System.out.println(user);//在User类里加上toString才能打印出属性，否则打印出：com.heima.kafka.pojo.User@2f8d0d02
        }

    }
}
